﻿using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

using ZhuXingFamilys.Help;
using ZhuXingFamilys.utils;

namespace ZhuXingFamilys.User
{
    /// <summary>
    /// UserCenter.xaml 的交互逻辑
    /// </summary>
    public partial class UserCenter : Window
    {
        public UserCenter()
        {
            ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Light);
            Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();
            UserLogininfo();
        }

        public const string CONFIG_LOGIN_CONTENT = "LoginInfo";
        public const string CONFIG_LOGIN_USERNAME_CONTENT = "UserName";
        public const string CONFIG_LOGIN_PASS_CONTENT = "UserPassword";
        public const string CONFIG_LOGIN_LOGINTIME_CONTENT = "LoginTime";
        public const string CONFIG_LOGIN_IsRemember_CONTENT = "IsRemember";
        public const string CONFIG_LOGIN_Authority_CONTENT = "Authority";
        public static string UserName;
        public static string UserPassword;
        public static string LoginTime;
        public static string Authority;
        public static int author = 0;
        public void UserLogininfo()
        {
            string iniFilePath = "LoginInfo.ini";
            string CurAppDir = Set.getaddress();
            //配置文件路径是相对路径则需加上当前exe所在路径作为根路径
            if (!iniFilePath.Contains("\\") || !Path.IsPathRooted(iniFilePath))
            {
                iniFilePath = Path.Combine(CurAppDir, iniFilePath);
            }
            UserName = Set.GetValue(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_USERNAME_CONTENT, iniFilePath, out string usernamevalue);
            LoginTime = Set.GetValue(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_LOGINTIME_CONTENT, iniFilePath, out string ctimevalue);
            Authority = Set.GetValue(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_Authority_CONTENT, iniFilePath, out string authvalue);
            try
            {
                author = int.Parse(Authority);
            }
            catch (Exception)
            {

            }

            string auth = string.Empty;
            switch (author)
            {
                case 0:
                    auth = "全部";
                    break;
                case 1:
                    auth = "筑星族库";
                    break;
                case 2:
                    auth = "雁伴信息处理平台";
                    break;
                default:
                    auth = "无";
                    break;
            }

            username.Content = "用户名： " + UserName;
            authority.Content = "插件使用权限： " + auth;
            logintime.Content = "上次登录时间： " + LoginTime;
        }
        // 退出登录
        private void Quit_Button(object sender, RoutedEventArgs e)
        {
            LogOut();
            this.Close();

        }
        public static void LogOut()
        {
            string iniFilePath = "LoginInfo.ini";
            string CurAppDir = Set.getaddress();
            //配置文件路径是相对路径则需加上当前exe所在路径作为根路径
            if (!iniFilePath.Contains("\\") || !Path.IsPathRooted(iniFilePath))
            {
                iniFilePath = Path.Combine(CurAppDir, iniFilePath);
            }
            Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_LOGINTIME_CONTENT, "", iniFilePath);
            Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_IsRemember_CONTENT, "", iniFilePath);
            Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_USERNAME_CONTENT, "", iniFilePath);
            Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_PASS_CONTENT, "", iniFilePath);
            Set.WritePrivateProfileString(CONFIG_LOGIN_CONTENT, CONFIG_LOGIN_Authority_CONTENT, "", iniFilePath);
            // 退出登录后禁用Panel
            PanelEnableDisableUtils.PanelDisable("筑星族库");
            PanelEnableDisableUtils.PanelDisable("雁伴毕姆");
        }
    }
}
